package com.fk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fk.reggie.common.R;
import com.fk.reggie.domain.Category;
import com.fk.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> add(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    public R<Page<Category>> page(Integer page, Integer pageSize) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Category::getSort);
        Page<Category> categorypage = new Page<Category>(page, pageSize);
        categoryService.page(categorypage, lambdaQueryWrapper);
        return R.success(categorypage);
    }

    @DeleteMapping
    public R<String> delete(Long ids) {
        categoryService.deleteCategory(ids);
        return R.success("删除成功");//1636203816974778369
    }


    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("删除成功");
    }

    //http://localhost:8080/category/list?type=1
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(category.getType() != null, Category::getType, category.getType())
                .orderByAsc(Category::getSort)
                .orderByAsc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return R.success(list);
    }
}
